<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: lzy
  Date: 2018/11/2
  Time: 15:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Add new Employee</title>
    <meta charset="utf-8">
    <%--<meta http-equiv="X-UA-Compatible" content="IE=edge">--%>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <!-- Bootstrap -->
    <link href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <%--jQuery--%>
    <script src="${APP_PATH}/static/jQuery/jquery.min-1.9.1.js"></script>
    <style>
        body {
            padding-top: 60px;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            $("#lastName").change(function () {
                var val = $(this).val();
                val = $.trim(val);
                $(this).val(val);

                //若修改的lastName和之前的lastName一致，则不发送Ajax请求，直接Alert：lastName可用
                var _oldLastName = $("#_oldLastName").val();
                _oldLastName = $.trim(_oldLastName);
                if (_oldLastName !== null && _oldLastName !== "" && _oldLastName === val) {
                    alert("lastName可用");
                    return;
                }

                var url = "${pageContext.request.contextPath}/ajaxValidateLastName";
                var args = {"lastName": val, "date": new Date()};

                $.post(url, args, function (data) {
                    if (data === "1") {
                        alert("用户名不可用");
                    } else if (data === "0") {
                        alert("用户名可以使用")
                    }
                    else {
                        alert("网络或程序出错");
                    }
                });
            });
        })
    </script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <a href="#" class="navbar-brand">Welcome to SSSP Test</a>
        </div>
    </div>
</nav>
<div class="container">
    <form:form action="${pageContext.request.contextPath}/emp" method="post" modelAttribute="employee"
               class="form-horizontal">
        <%--
            使用input标签传递隐藏域数据和使用form:hidden传递隐藏域信息判断的标准：
            如果隐藏域信息和modelAttribute内容相关，即如果隐藏域内容需要在modelAttribute中进行回显，则使用form:hidden标签，反之其他的隐藏域信息使用input标签传递
        --%>
        <c:if test="${employee.id != null}">
            <input type="hidden" id="_oldLastName" value="${employee.lastName}">
            <form:hidden path="id"/>
            <input type="hidden" name="_method" value="PUT">
        </c:if>

        <div class="form-group">
            <label for="lastName" class="col-sm-2 control-label">Last Name</label>
            <div class="col-sm-2">
                <form:input path="lastName" id="lastName" class="form-control"/>
            </div>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label">Email address</label>
            <div class="col-sm-2">
                <form:input path="email" id="email" class="form-control"/>
            </div>
        </div>
        <div class="form-group">
            <label for="birth" class="col-sm-2 control-label">Birth</label>
            <div class="col-sm-2">
                <form:input path="birth" id="birth" class="form-control"/>
            </div>
        </div>
        <div class="form-group">
            <label for="Department" class="col-sm-2 control-label">Department</label>
            <div class="col-sm-2">
                <form:select id="Department" class="form-control"
                             path="department.id" items="${departments}"
                             itemLabel="departmentName" itemValue="id"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="Submit" class="btn btn-primary"/>
            </div>
        </div>

    </form:form>
</div>
</body>
</html>
